import Mock from 'mockjs'
// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}
export let shopList = [
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店一店',
        type: '数码',
        address: '山东省-威海市',
        description: '这是一家好店',
        status: true,
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店二店',
        type: '数码',
        address: '四川省-成都市',
        description: '这是一家好店',
        status: true
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店三店',
        type: '数码',
        address: '吉林省-长春市',
        description: '这是一家好店',
        status: true
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店四店',
        type: '数码',
        address: '安徽省-合肥市',
        description: '这是一家好店',
        status: true
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店五店',
        type: '数码',
        address: '广东省-广州市',
        description: '这是一家好店',
        status: true
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店六店',
        type: '数码',
        address: '福建省-厦门市',
        description: '这是一家好店',
        status: true
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店七店',
        type: '数码',
        address: '浙江省-杭州市',
        description: '这是一家好店',
        status: true
    },
    {
        id: Mock.Random.guid(),
        name: '数码旗舰店八店',
        type: '数码',
        address: '江西省-上饶市',
        description: '这是一家好店',
        status: true
    },
]

export default {

    /**
      * 增加用户
      * @param name, addr, age, birth, sex
      * @return {{code: number, data: {message: string}}}
      */
    createShop: config => {
        const { name, address, type, description, } = JSON.parse(config.body)
        console.log(JSON.parse(config.body))
        shopList.unshift({
            id: Mock.Random.guid(),
            name: name,
            address: address,
            type: type,
            description: description
        })
        return {
            code: 20000,
            data: {
                message: '添加成功'
            }
        }
    },
    /**
     * 删除用户
     * @param id
     * @return {*}
     */
    deleteShop: config => {
        const { id } = JSON.parse(config.body)
        if (!id) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            shopList = shopList.filter(u => u.id !== id)
            return {
                code: 20000,
                message: '删除成功'
            }
        }
    },
    /**
     * 修改用户
     * @param username, password,
     * @return {{code: number, data: {message: string}}}
     */
    updateShop: config => {
        const { name, id, description, type, address } = JSON.parse(config.body)
        shopList.some(u => {
            if (u.id === id) {
                u.name = name
                u.description = description
                u.type = type
                u.address = address
                return true
            }
        })
        return {
            code: 20000,
            data: {
                message: '编辑成功'
            }
        }
    }
}
